package com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.repository.HddcWyGeochemicalsvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.repository.HddcWyGeochemicalsvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.repository.entity.HddcWyGeochemicalsvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.service.HddcWyGeochemicalsvypointService;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.viewobjects.HddcWyGeochemicalsvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyPoint.viewobjects.HddcWyGeochemicalsvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-02
 */
@Service
public class HddcWyGeochemicalsvypointServiceImpl implements HddcWyGeochemicalsvypointService {

	@Autowired
    private HddcWyGeochemicalsvypointRepository hddcWyGeochemicalsvypointRepository;
    @Autowired
    private HddcWyGeochemicalsvypointNativeRepository hddcWyGeochemicalsvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyGeochemicalsvypoints(HddcWyGeochemicalsvypointQueryParams queryParams , int curPage,
                                                      int pageSize) {
        Page<HddcWyGeochemicalsvypointEntity> hddcWyGeochemicalsvypointPage =
                this.hddcWyGeochemicalsvypointNativeRepository.queryHddcWyGeochemicalsvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeochemicalsvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeochemicalsvypointEntity getHddcWyGeochemicalsvypoint(String id) {
        HddcWyGeochemicalsvypointEntity hddcWyGeochemicalsvypoint = this.hddcWyGeochemicalsvypointRepository.findById(id).orElse(null);
         return hddcWyGeochemicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeochemicalsvypointEntity saveHddcWyGeochemicalsvypoint(HddcWyGeochemicalsvypointEntity hddcWyGeochemicalsvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeochemicalsvypoint.setId(uuid);
        hddcWyGeochemicalsvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeochemicalsvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeochemicalsvypointRepository.save(hddcWyGeochemicalsvypoint);
        return hddcWyGeochemicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeochemicalsvypointEntity updateHddcWyGeochemicalsvypoint(HddcWyGeochemicalsvypointEntity hddcWyGeochemicalsvypoint) {
        hddcWyGeochemicalsvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeochemicalsvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeochemicalsvypointRepository.save(hddcWyGeochemicalsvypoint);
        return hddcWyGeochemicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeochemicalsvypoints(List<String> ids) {
        List<HddcWyGeochemicalsvypointEntity> hddcWyGeochemicalsvypointList = this.hddcWyGeochemicalsvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeochemicalsvypointList) && hddcWyGeochemicalsvypointList.size() > 0) {
            for(HddcWyGeochemicalsvypointEntity hddcWyGeochemicalsvypoint : hddcWyGeochemicalsvypointList) {
                this.hddcWyGeochemicalsvypointRepository.delete(hddcWyGeochemicalsvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeochemicalsvypoint(HddcWyGeochemicalsvypointQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeochemicalsvypointNativeRepository.queryHddcWyGeochemicalsvypoint(queryParams);
        return bigInteger;
    }

}
